package cn.sunsine.test.lang;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

/**
 * String使用demo
 */
public class StringDemo {

    public static void main(String[] args) throws UnsupportedEncodingException {
        String s = "中文字符串";
        String s1 = "hello world";
        System.out.println("s1=" + s1);
        System.out.println("=========================构造函数====================================");
        String s2 = new String("Hello World");
        System.out.println("s2=" + s2);
        char[] chars = {'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'};
        String s3 = new String(chars);
        System.out.println("s3=" + s3);
        String s4 = new String(chars, 0, 5);
        System.out.println("s4=" + s4);
        int[] ints = {91, 81, 97, 99, 98};
        System.out.println((char) 88);
        String s5 = new String(ints, 1, 3);
        System.out.println("s5=" + s5);
        System.out.println("===========================方法==================================");
        // 计算字符串长度
        System.out.println("String length：" + s1.length());
        // 字符串尾部拼接
        System.out.println("String concat=" + s4.concat("World"));
        // 判断是否包含指定字符串
        System.out.println("String contains=" + s2.contains("World"));
        System.out.println("String charAt=" + s4.charAt(0));
        System.out.println("String codePointAt=" + s4.codePointAt(0));
        System.out.println("String codePointBefore=" + s4.codePointBefore(2));
        System.out.println("String codePointCount=" + s4.codePointCount(0, 3));
        System.out.println("String codePointCount=" + s.getBytes());
        System.out.println("String codePointCount=" + s.getBytes(Charset.forName("UTF-8")));
        System.out.println("String codePointCount=" + s.getBytes("UTF-8"));
        // 判断字符串对象是否以指定的str开头
        System.out.println("String startsWith=" + s1.startsWith("h"));
        System.out.println("String startsWith2=" + s1.startsWith("e", 1));
        System.out.println("String indexOf=" + s1.indexOf("e"));
        System.out.println("String substring=" + s1.substring(3));
        System.out.println("String substring2=" + s1.substring(3, 6));
    }
}
